package com.shop.filter;

import com.shop.pajo.Admin;
import com.shop.utils.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AdminFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        String servletPath = request.getServletPath();
        String method = request.getParameter("method");
        //从Session中获取用户
        Admin admin=(Admin)request.getSession().getAttribute(Constants.ADMIN_SESSION);
        if(method != null && method.equals("login") || servletPath.equals("/jsp/admin/adminLogin.jsp")){
            filterChain.doFilter(servletRequest, servletResponse);
        }else {
            if(admin == null){//用户已经被注销
                response.sendRedirect("/error.jsp");
            }else{
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }
    }
}
